﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.EmpbasicService;
using CommunitySystem.JuZhuanAnZhiService;

namespace CommunitySystem
{
    public partial class JuZhuanAnZhiJiHuaEmpControl : UserControl
    {
        private JuZhuanAnZhiServiceClient Client =
            new JuZhuanAnZhiServiceClient();
        private ObservableCollection<JuZhuanAnZhiJiHuaModel> poi =
            new ObservableCollection<JuZhuanAnZhiJiHuaModel>();
        private ObservableCollection<JuZhuanAnZhiJiHuaEmpModel> selectedempmodel =
           new ObservableCollection<JuZhuanAnZhiJiHuaEmpModel>();
        private EmpbasicService.EmpBasicServiceClient empclient = new EmpbasicService.EmpBasicServiceClient();
        public ObservableCollection<BianZhiModel> BianZhiModels = new ObservableCollection<BianZhiModel>();
        public JuZhuanAnZhiJiHuaEmpControl()
        {
            InitializeComponent();

            //for (int i = DateTime.Today.Year; i > DateTime.Today.Year - 10; i--)
            //{
            //    ComboBoxItem cbi = new ComboBoxItem();
            //    cbi.Content = i.ToString();
            //    cbi.Tag = i;
            //    nian.Items.Add(cbi);
            //}
            //nian.SelectedIndex = 0;
            Client.GetZhaoLuJiHuaModelForComCompleted += new EventHandler<GetZhaoLuJiHuaModelForComCompletedEventArgs>(Client1_GetZhaoLuJiHuaModelForComCompleted);
            Client.GetZhaoLuJiHuaModelForComAsync();
            Client.DeleteJuZhuanAnZhiJiHuaEmpCompleted += new EventHandler<DeleteJuZhuanAnZhiJiHuaEmpCompletedEventArgs>(Client_DeleteJuZhuanAnZhiJiHuaEmpCompleted);
            Client.QueRenJuZhuanAnZhiJiHuaEmpModelCompleted += new EventHandler<QueRenJuZhuanAnZhiJiHuaEmpModelCompletedEventArgs>(Client_QueRenJuZhuanAnZhiJiHuaEmpModelCompleted);
            Client.GetJuZhuanAnZhiJiHuaEmpModelCompleted +=
               new EventHandler<GetJuZhuanAnZhiJiHuaEmpModelCompletedEventArgs>(Client_GetJuZhuanAnZhiJiHuaEmpModelCompleted);
            empclient.GetBianZhiDetailModelCompleted += new EventHandler<GetBianZhiDetailModelCompletedEventArgs>(Client2_GetBianZhiDetailModelCompleted);
            //empclient.GetBianZhiDetailModelCompleted += new EventHandler<GetBianZhiDetailModelCompletedEventArgs>(Client2_GetBianZhiDetailModelCompleted);
            Load();
        }
        //void Client2_GetBianZhiDetailModelCompleted(object sender, GetBianZhiDetailModelCompletedEventArgs e)
        //{
        //    BianZhiModels = e.Result;
        //    if (BianZhiModels != null && BianZhiModels.Count > 0)
        //    {
        //        string zhurenkeyong = "";
        //        int i1 =
        //            Convert.ToInt32(
        //                !string.IsNullOrEmpty(BianZhiModels.OrderBy(p => p.FuKeLingDao).FirstOrDefault().FuKeLingDao)
        //                    ? BianZhiModels.OrderBy(p => p.FuKeLingDao).FirstOrDefault().FuKeLingDao
        //                    : "0");
        //        int i2 =
        //            Convert.ToInt32(
        //                !string.IsNullOrEmpty(
        //                    BianZhiModels.OrderByDescending(p => p.zhurenshiyou).FirstOrDefault().zhurenshiyou)
        //                    ? BianZhiModels.OrderByDescending(p => p.zhurenshiyou).FirstOrDefault().zhurenshiyou
        //                    : "0");

        //        int i3 = 0;
        //        foreach (BianZhiModel b in BianZhiModels)
        //        {
        //            if (!string.IsNullOrEmpty(b.ZhuRenJieYongQingKuang) && b.ZhuRenJieYongQingKuang != "无")
        //            {
        //                if (Math.Abs(Convert.ToInt32(b.ZhuRenJieYongQingKuang)) > i3)
        //                    i3 = Convert.ToInt32(b.ZhuRenJieYongQingKuang);
        //            }
        //        }
        //        int i4 =
        //            Convert.ToInt32(
        //                BianZhiModels.OrderByDescending(p => p.ZhuRenChaoZhiQingKuang).FirstOrDefault().
        //                    ZhuRenChaoZhiQingKuang == "无"
        //                    ? "0"
        //                    : BianZhiModels.OrderByDescending(p => p.ZhuRenChaoZhiQingKuang).FirstOrDefault().
        //                          ZhuRenChaoZhiQingKuang);
        //        zhurenkeyong = (i1 - i2 + i3 + i4).ToString();

        //        string fuzhurenkeyong = "";
        //        int f1 =
        //            Convert.ToInt32(
        //                !string.IsNullOrEmpty(
        //                    BianZhiModels.OrderBy(p => p.FuKeFeiLingDao).FirstOrDefault().FuKeFeiLingDao)
        //                    ? BianZhiModels.OrderBy(p => p.FuKeFeiLingDao).FirstOrDefault().FuKeFeiLingDao
        //                    : "0");
        //        int f2 =
        //            Convert.ToInt32(
        //                !string.IsNullOrEmpty(
        //                    BianZhiModels.OrderByDescending(p => p.fuzhurenshiyou).FirstOrDefault().fuzhurenshiyou)
        //                    ? BianZhiModels.OrderByDescending(p => p.fuzhurenshiyou).FirstOrDefault().fuzhurenshiyou
        //                    : "0");

        //        int f3 = 0;
        //        foreach (BianZhiModel b in BianZhiModels)
        //        {
        //            if (!string.IsNullOrEmpty(b.FuZhuRenJieYongQingKuang) && b.FuZhuRenJieYongQingKuang != "无")
        //            {
        //                if (Math.Abs(Convert.ToInt32(b.FuZhuRenJieYongQingKuang)) > f3)
        //                    f3 = Convert.ToInt32(b.FuZhuRenJieYongQingKuang);
        //            }
        //        }
        //        int f4 =
        //            Convert.ToInt32(
        //                BianZhiModels.OrderByDescending(p => p.FuZhuRenChaoZhiQingKuang).FirstOrDefault().
        //                    FuZhuRenChaoZhiQingKuang == "无"
        //                    ? "0"
        //                    : BianZhiModels.OrderByDescending(p => p.FuZhuRenChaoZhiQingKuang).FirstOrDefault().
        //                          FuZhuRenChaoZhiQingKuang);
        //        fuzhurenkeyong = (f1 - f2 + f3 + f4).ToString();
        //        if (BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Count() > 0)
        //        {
        //            foreach (BianZhiModel i in BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true))
        //            {
        //                int zhiwhu = !string.IsNullOrEmpty(i.ZhengKeLingDao) ? Convert.ToInt32(i.ZhengKeLingDao) : 0;
        //                int renshu = BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Sum(
        //                    p => !string.IsNullOrEmpty(p.zhengkeshiyou) ? Convert.ToInt32(p.zhengkeshiyou) : 0);
        //                int jieyong = BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Sum(
        //                    p =>
        //                    p.ZhengKeJieYongQingKuang != "无" ? Convert.ToInt32(p.ZhengKeJieYongQingKuang) : 0);
        //                int chaozhi = BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Sum(
        //                    p =>
        //                    p.ZhengKeChaoZhiQingKuang != "无" ? Convert.ToInt32(p.ZhengKeChaoZhiQingKuang) : 0);

        //                i.ZhengKeShiJi =
        //                    (zhiwhu - renshu + jieyong + chaozhi)
        //                        .ToString();
        //            }
        //        }

        //        if (BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Count() > 0)
        //        {
        //            foreach (BianZhiModel i in BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true))
        //            {
        //                i.FuKeShiJi =
        //                    (Convert.ToInt32(
        //                        BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).OrderBy(p => p.FuKeShiJi).
        //                            FirstOrDefault().FuKeShiJi) +
        //                     BianZhiModels.Sum(
        //                         p => p.FuKeJieYongQingKuang != "无" ? Convert.ToInt32(p.FuKeJieYongQingKuang) : 0) +
        //                     BianZhiModels.Sum(
        //                         p => p.FuKeChaoZhiQingKuang != "无" ? Convert.ToInt32(p.FuKeChaoZhiQingKuang) : 0))
        //                        .ToString();

        //                int zhiwhu = !string.IsNullOrEmpty(i.ZhengKeFeiLingDao)
        //                                 ? Convert.ToInt32(i.ZhengKeFeiLingDao)
        //                                 : 0;
        //                int renshu = BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Sum(
        //                    p => !string.IsNullOrEmpty(p.fukeshiyou) ? Convert.ToInt32(p.fukeshiyou) : 0);
        //                int jieyong = BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Sum(
        //                    p =>
        //                    p.FuKeJieYongQingKuang != "无" ? Convert.ToInt32(p.FuKeJieYongQingKuang) : 0);
        //                int chaozhi = BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Sum(
        //                    p =>
        //                    p.FuKeChaoZhiQingKuang != "无" ? Convert.ToInt32(p.FuKeChaoZhiQingKuang) : 0);

        //                i.ZhengKeShiJi =
        //                    (zhiwhu - renshu + jieyong + chaozhi)
        //                        .ToString();
        //            }
        //        }
        //        foreach (BianZhiModel i in BianZhiModels)
        //        {
        //            i.ZhuRenShiJi = zhurenkeyong;
        //            i.FuZhuRenShiJi = fuzhurenkeyong;
        //        }

        //        foreach (BianZhiModel i in BianZhiModels)
        //        {
        //            if (i.OrgID == selectedempmodel[0].AnZhiDanWei &&
        //                Convert.ToInt32(!string.IsNullOrEmpty(i.ZhuRenShiJi) ? i.ZhuRenShiJi : "0") <= zhurencount)
        //            {
        //                SilverlightMessageBoxLibrary.Message.ErrorMessage("岗位职数不足，请与主管部门联系！");
        //                return;
        //            }
        //        }
        //        foreach (BianZhiModel i in BianZhiModels)
        //        {
        //            if (i.OrgID == selectedempmodel[0].AnZhiDanWei &&
        //                Convert.ToInt32(!string.IsNullOrEmpty(i.FuZhuRenShiJi) ? i.FuZhuRenShiJi : "0") <= fuzhurencount)
        //            {
        //                SilverlightMessageBoxLibrary.Message.ErrorMessage("岗位职数不足，请与主管部门联系！");
        //                return;
        //            }
        //        }
        //        if (MessageBox.Show("确定安置所选人员吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
        //        {
        //            JuZhuanAnZhiJiHuaDateWindow win = new JuZhuanAnZhiJiHuaDateWindow();
        //            win.Closed += new EventHandler(win_Closed);
        //            win.Show();
        //        }
        //    }
        //    else
        //    {
        //        SilverlightMessageBoxLibrary.Message.ErrorMessage("该单位没有内设科室！");
        //        return;
        //    }
        //}
        void Client2_GetBianZhiDetailModelCompleted(object sender, GetBianZhiDetailModelCompletedEventArgs e)
        {
            BianZhiModels = e.Result;
            if (BianZhiModels != null && BianZhiModels.Count > 0)
            {
                string zhurenkeyong = "";
                int i1 =
                    Convert.ToInt32(
                        !string.IsNullOrEmpty(BianZhiModels.OrderBy(p => p.FuKeLingDao).FirstOrDefault().FuKeLingDao)
                            ? BianZhiModels.OrderBy(p => p.FuKeLingDao).FirstOrDefault().FuKeLingDao
                            : "0");
                int i2 =
                    Convert.ToInt32(
                        !string.IsNullOrEmpty(
                            BianZhiModels.OrderByDescending(p => p.zhurenshiyou).FirstOrDefault().zhurenshiyou)
                            ? BianZhiModels.OrderByDescending(p => p.zhurenshiyou).FirstOrDefault().zhurenshiyou
                            : "0");

                int i3 = 0;
                foreach (BianZhiModel b in BianZhiModels)
                {
                    if (!string.IsNullOrEmpty(b.ZhuRenJieYongQingKuang) && b.ZhuRenJieYongQingKuang != "无")
                    {
                        if (Math.Abs(Convert.ToInt32(b.ZhuRenJieYongQingKuang)) > i3)
                            i3 = Convert.ToInt32(b.ZhuRenJieYongQingKuang);
                    }
                }
                int i4 =
                    Convert.ToInt32(
                        BianZhiModels.OrderByDescending(p => p.ZhuRenChaoZhiQingKuang).FirstOrDefault().
                            ZhuRenChaoZhiQingKuang == "无"
                            ? "0"
                            : BianZhiModels.OrderByDescending(p => p.ZhuRenChaoZhiQingKuang).FirstOrDefault().
                                  ZhuRenChaoZhiQingKuang);
                zhurenkeyong = (i1 - i2 + i3 + i4).ToString();

                string fuzhurenkeyong = "";
                int f1 =
                    Convert.ToInt32(
                        !string.IsNullOrEmpty(
                            BianZhiModels.OrderBy(p => p.FuKeFeiLingDao).FirstOrDefault().FuKeFeiLingDao)
                            ? BianZhiModels.OrderBy(p => p.FuKeFeiLingDao).FirstOrDefault().FuKeFeiLingDao
                            : "0");
                int f2 =
                    Convert.ToInt32(
                        !string.IsNullOrEmpty(
                            BianZhiModels.OrderByDescending(p => p.fuzhurenshiyou).FirstOrDefault().fuzhurenshiyou)
                            ? BianZhiModels.OrderByDescending(p => p.fuzhurenshiyou).FirstOrDefault().fuzhurenshiyou
                            : "0");

                int f3 = 0;
                foreach (BianZhiModel b in BianZhiModels)
                {
                    if (!string.IsNullOrEmpty(b.FuZhuRenJieYongQingKuang) && b.FuZhuRenJieYongQingKuang != "无")
                    {
                        if (Math.Abs(Convert.ToInt32(b.FuZhuRenJieYongQingKuang)) > f3)
                            f3 = Convert.ToInt32(b.FuZhuRenJieYongQingKuang);
                    }
                }
                int f4 =
                    Convert.ToInt32(
                        BianZhiModels.OrderByDescending(p => p.FuZhuRenChaoZhiQingKuang).FirstOrDefault().
                            FuZhuRenChaoZhiQingKuang == "无"
                            ? "0"
                            : BianZhiModels.OrderByDescending(p => p.FuZhuRenChaoZhiQingKuang).FirstOrDefault().
                                  FuZhuRenChaoZhiQingKuang);
                fuzhurenkeyong = (f1 - f2 + f3 + f4).ToString();
                if (BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Count() > 0)
                {
                    foreach (BianZhiModel i in BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true))
                    {
                        int zhiwhu = !string.IsNullOrEmpty(i.ZhengKeLingDao) ? Convert.ToInt32(i.ZhengKeLingDao) : 0;
                        int renshu = BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Sum(
                            p => !string.IsNullOrEmpty(p.zhengkeshiyou) ? Convert.ToInt32(p.zhengkeshiyou) : 0);
                        int jieyong = BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Sum(
                            p =>
                            p.ZhengKeJieYongQingKuang != "无" ? Convert.ToInt32(p.ZhengKeJieYongQingKuang) : 0);
                        int chaozhi = BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Sum(
                            p =>
                            p.ZhengKeChaoZhiQingKuang != "无" ? Convert.ToInt32(p.ZhengKeChaoZhiQingKuang) : 0);

                        i.ZhengKeShiJi =
                            (zhiwhu - renshu + jieyong + chaozhi)
                                .ToString();
                    }
                }

                if (BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Count() > 0)
                {
                    foreach (BianZhiModel i in BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true))
                    {
                        i.FuKeShiJi =
                            (Convert.ToInt32(
                                BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).OrderBy(p => p.FuKeShiJi).
                                    FirstOrDefault().FuKeShiJi) +
                             BianZhiModels.Sum(
                                 p => p.FuKeJieYongQingKuang != "无" ? Convert.ToInt32(p.FuKeJieYongQingKuang) : 0) +
                             BianZhiModels.Sum(
                                 p => p.FuKeChaoZhiQingKuang != "无" ? Convert.ToInt32(p.FuKeChaoZhiQingKuang) : 0))
                                .ToString();

                        int zhiwhu = !string.IsNullOrEmpty(i.ZhengKeFeiLingDao)
                                         ? Convert.ToInt32(i.ZhengKeFeiLingDao)
                                         : 0;
                        int renshu = BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Sum(
                            p => !string.IsNullOrEmpty(p.fukeshiyou) ? Convert.ToInt32(p.fukeshiyou) : 0);
                        int jieyong = BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Sum(
                            p =>
                            p.FuKeJieYongQingKuang != "无" ? Convert.ToInt32(p.FuKeJieYongQingKuang) : 0);
                        int chaozhi = BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Sum(
                            p =>
                            p.FuKeChaoZhiQingKuang != "无" ? Convert.ToInt32(p.FuKeChaoZhiQingKuang) : 0);

                        i.ZhengKeShiJi =
                            (zhiwhu - renshu + jieyong + chaozhi)
                                .ToString();
                    }
                }
                foreach (BianZhiModel i in BianZhiModels)
                {
                    i.ZhuRenShiJi = zhurenkeyong;
                    i.FuZhuRenShiJi = fuzhurenkeyong;
                }
                if (BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Count() > 0)
                {
                    foreach (BianZhiModel i in BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true))
                    {
                        int zhiwhu = !string.IsNullOrEmpty(i.ZhengKeLingDao) ? Convert.ToInt32(i.ZhengKeLingDao) : 0;
                        int renshu = BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Sum(
                            p => !string.IsNullOrEmpty(p.zhengkeshiyou) ? Convert.ToInt32(p.zhengkeshiyou) : 0);
                        int jieyong = BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Sum(
                            p =>
                            p.ZhengKeJieYongQingKuang != "无" ? Convert.ToInt32(p.ZhengKeJieYongQingKuang) : 0);
                        int chaozhi = BianZhiModels.Where(p => p.ZhengKeLingDaoTC == true).Sum(
                            p =>
                            p.ZhengKeChaoZhiQingKuang != "无" ? Convert.ToInt32(p.ZhengKeChaoZhiQingKuang) : 0);

                        i.ZhengKeShiJi =
                            (zhiwhu - renshu + jieyong + chaozhi)
                                .ToString();
                    }
                }

                if (BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Count() > 0)
                {
                    foreach (BianZhiModel i in BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true))
                    {
                        i.FuKeShiJi =
                            (Convert.ToInt32(
                                BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).OrderBy(p => p.FuKeShiJi).
                                    FirstOrDefault().FuKeShiJi) +
                             BianZhiModels.Sum(
                                 p => p.FuKeJieYongQingKuang != "无" ? Convert.ToInt32(p.FuKeJieYongQingKuang) : 0) +
                             BianZhiModels.Sum(
                                 p => p.FuKeChaoZhiQingKuang != "无" ? Convert.ToInt32(p.FuKeChaoZhiQingKuang) : 0))
                                .ToString();

                        int zhiwhu = !string.IsNullOrEmpty(i.ZhengKeFeiLingDao) ? Convert.ToInt32(i.ZhengKeFeiLingDao) : 0;
                        int renshu = BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Sum(
                            p => !string.IsNullOrEmpty(p.fukeshiyou) ? Convert.ToInt32(p.fukeshiyou) : 0);
                        int jieyong = BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Sum(
                            p =>
                            p.FuKeJieYongQingKuang != "无" ? Convert.ToInt32(p.FuKeJieYongQingKuang) : 0);
                        int chaozhi = BianZhiModels.Where(p => p.ZhengKeFeiLingDaoTC == true).Sum(
                            p =>
                            p.FuKeChaoZhiQingKuang != "无" ? Convert.ToInt32(p.FuKeChaoZhiQingKuang) : 0);

                        i.ZhengKeShiJi =
                            (zhiwhu - renshu + jieyong + chaozhi)
                                .ToString();
                    }
                }
                if (Validate())
                {
                    if (MessageBox.Show("确定安置所选人员吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                    {
                        JuZhuanAnZhiJiHuaDateWindow win = new JuZhuanAnZhiJiHuaDateWindow();
                        win.Closed += new EventHandler(win_Closed);
                        win.Show();
                    }
                }

            }
        }
        void gangwei_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (((ComboBox)sender).SelectedItem != null)
                ExamID = ((JuZhuanAnZhiJiHuaModel)((((ComboBox)sender).SelectedItem))).ID;
            else ExamID = 0;
        }
        private int ExamID = -1;
        void Client1_GetZhaoLuJiHuaModelForComCompleted(object sender, GetZhaoLuJiHuaModelForComCompletedEventArgs e)
        {
            poi = e.Result;
            biaoti.ItemsSource = poi;
            if (poi.Count > 0) biaoti.SelectedIndex = 0;
            Load();
        }

        public void Load()
        {
            Client.GetJuZhuanAnZhiJiHuaEmpModelAsync(ExamID, "", 0);
        }

        void Client_GetJuZhuanAnZhiJiHuaEmpModelCompleted(object sender, GetJuZhuanAnZhiJiHuaEmpModelCompletedEventArgs e)
        {
            DGRole.ItemsSource = e.Result;
        }

        private string islock = "0";
        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            biaoti.ItemsSource = null;
            islock = "1";
            ObservableCollection<JuZhuanAnZhiJiHuaModel> poi1 = new ObservableCollection<JuZhuanAnZhiJiHuaModel>();
            foreach (JuZhuanAnZhiJiHuaModel i in poi)
            {
                if (i.IsLocked) poi1.Add(i);
            }
            if (poi1.Count > 0 && poi1[0] != null)
            {
                biaoti.ItemsSource = poi1;
                if (poi1.Count > 0) biaoti.SelectedIndex = 0;
            }
            Load();
        }

        private void chkHiddenCols_Unchecked(object sender, RoutedEventArgs e)
        {
            biaoti.ItemsSource = null;
            islock = "0";
            ObservableCollection<JuZhuanAnZhiJiHuaModel> poi1 = new ObservableCollection<JuZhuanAnZhiJiHuaModel>();
            //if (poi.Where(p => p.Status != 3).FirstOrDefault() != null) poi1.Add(poi.Where(p => p.Status != 3).FirstOrDefault());
            foreach (JuZhuanAnZhiJiHuaModel p in poi)
            {
                if (p.IsLocked == false) poi1.Add(p);
            }
            if (poi1.Count > 0 && poi1[0] != null)
            {
                biaoti.ItemsSource = poi1;
                if (poi1.Count > 0) biaoti.SelectedIndex = 0;
            }
            else
            {
                biaoti.ItemsSource = null;
            }
            Load();
        }


        private void NewData_Click(object sender, RoutedEventArgs e)
        {
            JuZhuanAnZhiJiHuaEmpWindow RMWindow = new JuZhuanAnZhiJiHuaEmpWindow(0, true,ExamID);
            RMWindow.Closed += new EventHandler(RMWindow_Closed);
            RMWindow.Show();
        }
        void RMWindow_Closed(object sender, EventArgs e)
        {
            Load();
        }

        private void EditData_Click(object sender, RoutedEventArgs e)
        {
            if (DGRole.ItemsSource != null)
            {
                foreach (JuZhuanAnZhiJiHuaEmpModel i in (ObservableCollection<JuZhuanAnZhiJiHuaEmpModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                if (selectedempmodel.Count > 1)
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("只可以选择一个人员进行编辑！");
                    return;
                }
                if (selectedempmodel[0].Status == 1)
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("所选择的人员有的已被安置，不可编辑！");
                    return;
                }
                JuZhuanAnZhiJiHuaEmpWindow RMWindow = new JuZhuanAnZhiJiHuaEmpWindow(selectedempmodel[0].ID, false, ExamID);
                RMWindow.Closed += new EventHandler(RMWindow_Closed);
                RMWindow.Show();

            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要编辑的人员！");
        }

        private void DeleteData_Click(object sender, RoutedEventArgs e)
        {
            if (DGRole.ItemsSource != null)
            {
                foreach (JuZhuanAnZhiJiHuaEmpModel i in (ObservableCollection<JuZhuanAnZhiJiHuaEmpModel>)DGRole.ItemsSource)
                {
                    if (i.Status == 1)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("所选择的人员有的已被安置，不可删除！");
                        selectedempmodel.Clear();
                        return;
                    }
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                if (MessageBox.Show("确定删除吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    Client.DeleteJuZhuanAnZhiJiHuaEmpAsync(selectedempmodel[0].ID);
                }
                else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要删除的人员！");
            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要删除的人员！");
        }
        void Client_DeleteJuZhuanAnZhiJiHuaEmpCompleted(object sender, DeleteJuZhuanAnZhiJiHuaEmpCompletedEventArgs e)
        {
            Load();
        }
        int zhurencount = 0;
        int fuzhurencount = 0;
        private void btnSuoData_Click(object sender, RoutedEventArgs e)
        {
            selectedempmodel.Clear();
            
            if (DGRole.ItemsSource != null)
            {
                foreach (JuZhuanAnZhiJiHuaEmpModel i in (ObservableCollection<JuZhuanAnZhiJiHuaEmpModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
                
            }
            
            if (selectedempmodel.Count > 0)
            {
                int danwei = selectedempmodel[0].AnZhiDanWei;
                foreach (JuZhuanAnZhiJiHuaEmpModel i in selectedempmodel)
                {
                    if (i.Status == 1)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("所选择的人员有的已被安置，不可重复安置！");
                        selectedempmodel.Clear();
                        zhurencount = fuzhurencount = 0;
                        return;
                    }
                    if (i.AnZhiDanWei != danwei)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择同一安置单位进行人员安置！");
                        selectedempmodel.Clear();
                        zhurencount = fuzhurencount = 0;
                        return;
                    }
                    if(i.ZhiWuShuXing == "非领导职务")
                    {
                        if (i.ZhiWuCengCi == "乡科级正职（主任科员）") zhurencount += 1;
                        if (i.ZhiWuCengCi == "乡科级副职（副主任科员）") fuzhurencount += 1;
                    }
                }
                
                empclient.GetBianZhiDetailModelAsync(selectedempmodel[0].AnZhiDanWei.ToString(), "");
                
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要安置的人员！");
            }
        }
        private bool Validate()
        {
            if (selectedempmodel.Count > 0)
            {
                foreach (JuZhuanAnZhiJiHuaEmpModel Model in selectedempmodel)
                {
                    if (Model.ZhiWuCengCi == "乡科级正职（主任科员）" && Model.ZhiWuShuXing == "领导职务")
                    {
                        if (BianZhiModels.Where(p => p.OrgID == Model.AnZhiDanWei && Convert.ToInt32(!string.IsNullOrEmpty(p.ZhengKeShiJi) ? p.ZhengKeShiJi : "0") > 0).FirstOrDefault() == null)
                        {
                            SilverlightMessageBoxLibrary.Message.ErrorMessage("岗位职数不足，请与主管部门联系！");
                            return false;
                        }
                    }
                    if (Model.ZhiWuCengCi == "乡科级副职（副主任科员）" && Model.ZhiWuShuXing == "领导职务")
                    {
                        if (BianZhiModels.Where(p => p.OrgID == Model.AnZhiDanWei && Convert.ToInt32(!string.IsNullOrEmpty(p.FuKeShiJi) ? p.FuKeShiJi : "0") > 0).FirstOrDefault() == null)
                        {
                            SilverlightMessageBoxLibrary.Message.ErrorMessage("岗位职数不足，请与主管部门联系！");
                            return false;
                        }
                    }
                    if (Model.ZhiWuCengCi == "乡科级正职（主任科员）" && Model.ZhiWuShuXing == "非领导职务")
                    {
                        if (BianZhiModels.Where(p => p.OrgID == Model.AnZhiDanWei && Convert.ToInt32(!string.IsNullOrEmpty(p.ZhuRenShiJi) ? p.ZhuRenShiJi : "0") > 0).FirstOrDefault() == null)
                        {
                            SilverlightMessageBoxLibrary.Message.ErrorMessage("岗位职数不足，请与主管部门联系！");
                            return false;
                        }
                    }
                    if (Model.ZhiWuCengCi == "乡科级副职（副主任科员）" && Model.ZhiWuShuXing == "非领导职务")
                    {
                        if (BianZhiModels.Where(p => p.OrgID == Model.AnZhiDanWei && Convert.ToInt32(!string.IsNullOrEmpty(p.FuZhuRenShiJi) ? p.FuZhuRenShiJi : "0") > 0).FirstOrDefault() == null)
                        {
                            SilverlightMessageBoxLibrary.Message.ErrorMessage("岗位职数不足，请与主管部门联系！");
                            return false;
                        }
                    }
                }
                return true;
            }
            else return false;
        }

        void win_Closed(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((JuZhuanAnZhiJiHuaDateWindow)sender).Dt.ToString()) && Convert.ToDateTime(((JuZhuanAnZhiJiHuaDateWindow)sender).Dt) != Convert.ToDateTime("1900-01-01"))
            Client.QueRenJuZhuanAnZhiJiHuaEmpModelAsync(selectedempmodel, ((JuZhuanAnZhiJiHuaDateWindow)sender).Dt);
        }
        void Client_QueRenJuZhuanAnZhiJiHuaEmpModelCompleted(object sender, QueRenJuZhuanAnZhiJiHuaEmpModelCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("安置成功！");
            Load();
        }

        private void ChaData_Click(object sender, RoutedEventArgs e)
        {
            Load();
        }

        private void dgXQ_LoadingRow(object sender, DataGridRowEventArgs e)
        {
        }
        private void DaoChuData_Click(object sender, RoutedEventArgs e)
        {
        }

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            foreach (JuZhuanAnZhiJiHuaEmpModel p in DGRole.ItemsSource)
            {
                p.IsSelected = (bool)cb.IsChecked;
                //CheckBox cb1 = DGRole.Columns[0].GetCellContent(p) as CheckBox;
                //cb1.IsChecked = p.IsSelected;
                if (p.IsSelected)
                {
                    if (selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(p);
                    }
                }
                else
                {
                    if (selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault() != null)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault());
                    }
                }
            }

        }
    }
}
